package cn.itcast.goods.book.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.book.service.BookService;
import cn.itcast.goods.pager.PageBean;
import cn.mytools.commons.CommonUtils;
import cn.mytools.servlet.BaseServlet;

public class BookServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2315661637181408602L;
	private BookService bookService = new BookService();

	/**
	 * 获取当前页的页码
	 * 
	 * @param req
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if (param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch (RuntimeException e) {
			} // 如果param为abc强转为int会出异常
		}
		return pc;
	}

	/**
	 * 截取url，页面中的分页导航需要使用它做为超链接的目标
	 * 
	 * @param req
	 * @return
	 */
	private String getUrl(HttpServletRequest req) {
		// 等于 /项目名/Servlet路径?method=find&pc=3
		String url = req.getRequestURI() + "?" + req.getQueryString();
		int index = url.lastIndexOf("&pc=");
		if (index != -1) {
			url = url.substring(0, index);
		}
		return url;
	}

	/**
	 * 按分类查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCategory(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);

		String cid = req.getParameter("cid");

		PageBean<Book> pb = bookService.findByCategory(cid, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);

		return "f:/jsps/book/list.jsp";
	}

	/**
	 * 按作者查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);

		String author = req.getParameter("author");

		PageBean<Book> pb = bookService.findByAuthor(author, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);

		return "f:/jsps/book/list.jsp";
	}

	/**
	 * 按出版社查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByPress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);

		String press = req.getParameter("press");

		PageBean<Book> pb = bookService.findByPress(press, pc);

		pb.setUrl(url);
		req.setAttribute("pb", pb);

		return "f:/jsps/book/list.jsp";
	}

	/**
	 * 按书名查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);

		String bname = req.getParameter("bname");

		PageBean<Book> pb = bookService.findByBname(bname, pc);

		pb.setUrl(url);
		req.setAttribute("pb", pb);

		return "f:/jsps/book/list.jsp";
	}

	/**
	 * 多条件组合查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCombination(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);

		Book criteria = CommonUtils.toBean(req.getParameterMap(), Book.class);

		PageBean<Book> pb = bookService.findByCombination(criteria, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);

		return "f:/jsps/book/list.jsp";
	}

	/**
	 * 按bid查询
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String bid = req.getParameter("bid");

		Book book = bookService.findByBid(bid);

		req.setAttribute("book", book);

		return "f:/jsps/book/desc.jsp";
	}
}
